package com.sdgakj.rest;

import com.sdgakj.dto.base.ResCode;
import com.sdgakj.dto.base.ResResult;
import com.sdgakj.service.ServiceException;
import com.sdgakj.service.order.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springside.modules.web.MediaTypes;

@RestController
@RequestMapping(value = "/api/v1/order")
public class OrderRestController {

	private static Logger logger = LoggerFactory.getLogger(OrderRestController.class);

	@Autowired
    private OrderService orderService;

    /**
     * 创建订单   前端创建，默认总需捐赠笔数1，   募捐金额必填，用户数必传，type默认0，
     * 
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public ResResult create(@RequestParam(value = "userId", defaultValue = "0") Long userId,
                            @RequestParam(value = "authTel", defaultValue = "0") String authTel,
                            @RequestParam(value = "targetNum", defaultValue = "0") Integer targetNum,
                            @RequestParam(value = "realNum", defaultValue = "0") Integer realNum,
                            @RequestParam(value = "totalPrice", defaultValue = "0") Double totalPrice) { //使用的积分金额 v2.5.0
        logger.info("userId:" + userId + "   authTel:" + authTel
                + "   totalPrice:" + totalPrice);
        ResResult result = new ResResult();
		try {
            result.setData(orderService.create(userId, authTel, totalPrice, realNum, targetNum));
		} catch (ServiceException e) {
			result.setResCode(e.getResCode());
            result.setResMsg(e.getMessage());
            logger.info("I-[创建订单]业务错误", e);
		} catch (Exception e) {
			result.setResCode(ResCode.ERROR);
            logger.error("I-[创建订单]程序异常", e);
		}
		return result;
	}

    /**
     * 取消订单
     * 
     *
     * @return
     */
    @RequestMapping(value = "/cancel", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public ResResult cancel(@RequestParam(value = "orderId", defaultValue = "0") Long orderId) {
        logger.info("orderId:" + orderId);
        ResResult result = new ResResult();
        try {
            orderService.cancelOrder(orderId);
        } catch (ServiceException e) {
            result.setResCode(e.getResCode());
            logger.info("I-[创建订单]业务错误", e);
        } catch (Exception e) {
            result.setResCode(ResCode.ERROR);
            logger.error("I-[创建订单]程序异常", e);
        }
        return result;
    }
	
    /**
     * 订单取消退款
     * 
     *
     * @return
     */
    @RequestMapping(value = "/unrefund", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public ResResult unrefund(@RequestParam(value = "orderId", defaultValue = "0") Long orderId) {
        logger.info("orderId:" + orderId);
        ResResult result = new ResResult();
        try {
            orderService.unrefundOrder(orderId);
        } catch (ServiceException e) {
            result.setResCode(e.getResCode());
            logger.info("I-[创建订单]业务错误", e);
        } catch (Exception e) {
            result.setResCode(ResCode.ERROR);
            logger.error("I-[创建订单]程序异常", e);
        }
        return result;
    }

    /**
     * 订单投诉
     * 
     *
     * @return
     */
    @RequestMapping(value = "/complain", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public ResResult complain(@RequestParam(value = "orderId", defaultValue = "0") Long orderId,
                              @RequestParam(value = "reason", defaultValue = "0") String reason) {
        logger.info("orderId:" + orderId + "reason:" + reason);
        ResResult result = new ResResult();
        try {
            orderService.complainOrder(orderId, reason);
        } catch (ServiceException e) {
            result.setResCode(e.getResCode());
            logger.info("I-[创建订单]业务错误", e);
        } catch (Exception e) {
            result.setResCode(ResCode.ERROR);
            logger.error("I-[创建订单]程序异常", e);
        }
        return result;
    }

    /**
     * 订单取消退款
     * 
     *
     * @return
     */
    @RequestMapping(value = "/cancelOrder", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public ResResult cancelOrder(@RequestParam(value = "orderId", defaultValue = "0") Long orderId) {
        logger.info("orderId:" + orderId);
        ResResult result = new ResResult();
        try {
            orderService.cancelOrder(orderId);
        } catch (ServiceException e) {
            result.setResCode(e.getResCode());
            logger.info("I-[创建订单]业务错误", e);
        } catch (Exception e) {
            result.setResCode(ResCode.ERROR);
            logger.error("I-[创建订单]程序异常", e);
        }
        return result;
    }

    /**
     * 订单取消退款
     *
     *
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public ResResult deleteOrder(@RequestParam(value = "id", defaultValue = "0") Long id) {
        logger.info("id:" + id);
        ResResult result = new ResResult();
        try {
            orderService.deleteOrder(id);
        } catch (ServiceException e) {
            result.setResCode(e.getResCode());
            logger.info("I-[删除订单]业务错误", e);
        } catch (Exception e) {
            result.setResCode(ResCode.ERROR);
            logger.error("I-[删除订单]程序异常", e);
        }
        return result;
    }

}
